<template>
  <!-- 测量特性定义表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120">
      <el-card>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工序" prop="equipCode">
              <span>{{ formData.processCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备编码" prop="equipCode">
              <span>{{ formData.equipCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品型号" prop="equipName">
              <span>{{ formData.modelCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品描述" prop="equipName">
              <span>{{ formData.modelName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检具设备号" prop="equipName">
              <span>{{ formData.gageEquipMarkCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检具设备" prop="equipName">
              <span>{{ formData.gageEquipMarkName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特性项" prop="equipName">
              <span>{{ formData.paramName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参考标准" prop="equipName">
              <span>{{ formData.parameterStandards }}</span>
            </el-form-item>
          </el-col>

          <el-row>
            <el-col :span="3">
              <el-form-item prop="isRatioUp">
                <el-checkbox
                  :disabled="props.isReadonly"
                  v-model="formData.isRatioUp"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="ruleLowestDuration" label="控制线上限比例">
                <el-input
                  :disabled="props.isReadonly"
                  v-model="formData.controlRatioUp"
                  placeholder="控制线上限比例"
                  clearable
                  @input="numValidcontrolRatioUp()"
                  type="number"
                  :suffixIcon="() => '%'"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="ruleLowestDuration" label="控制线上限">
                <el-input
                  disabled
                  v-model="formData.controlUp"
                  placeholder="控制线上限"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="ruleLowestDuration" label="公差上限">
                <el-input
                  disabled
                  v-model="formData.upperLimitValue"
                  placeholder="公差上限"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="3">
              <el-form-item prop="isRatioUp">
                <el-checkbox
                  :disabled="props.isReadonly"
                  v-model="formData.isRatioDow"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="ruleLowestDuration" label="控制线下限比例">
                <el-input
                  :disabled="props.isReadonly"
                  v-model="formData.controlRatioDown"
                  placeholder="控制线上限比例"
                  clearable
                  @input="numValidcontrolRatioDown()"
                  type="number"
                  :suffixIcon="() => '%'"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="ruleLowestDuration" label="控制线上限">
                <el-input
                  disabled
                  v-model="formData.controlDown"
                  placeholder="控制线上限"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="ruleLowestDuration" label="公差下限">
                <el-input
                  disabled
                  v-model="formData.lowerLimitValue"
                  placeholder="公差下限"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-row>
      </el-card>
      <el-card style="margin-top: 10px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="" prop="isSingle">
              <el-checkbox
                :disabled="props.isReadonly"
                v-model="formData.isSingle"
                >单值图预警</el-checkbox
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="isAverage">
              <el-checkbox
                :disabled="props.isReadonly"
                v-model="formData.isAverage"
                >均值图预警</el-checkbox
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="isLong">
              <el-checkbox
                :disabled="props.isReadonly"
                v-model="formData.isLong"
                >长期综合过程能力趋势图预警
              </el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="isStage">
              <el-checkbox
                :disabled="props.isReadonly"
                v-model="formData.isStage"
                >阶段综合过程能力趋势图预警
              </el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="isFeatures">
              <el-checkbox
                :disabled="props.isReadonly"
                v-model="formData.isFeatures"
                >多份特定送检状态过程分析趋势图预警
              </el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "PCMPCMMEASUREFEATURESADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  processId: [],
  processCode: [],
  processName: [],
  equipId: [],
  equipCode: [],
  equipName: [],
  projectId: [],
  projectCode: [],
  projectName: [],
  modelId: [],
  modelCode: [],
  modelName: [],
  gageEquipId: [],
  gageEquipCode: [],
  gageEquipName: [],
  gageEquipMarkId: [],
  gageEquipMarkCode: [],
  gageEquipMarkName: [],
  paramId: [],
  paramCode: [],
  paramName: [],
  checkType: [],
  parameterStandards: [],
  lowerLimitValue: [],
  upperLimitValue: [],
  controlRatioUp: [],
  controlRatioDown: [],
  controlUp: [],
  controlDown: [],
  isRatioUp: [],
  isRatioDow: [],
  isSingle: [],
  isAverage: [],
  isLong: [],
  isStage: [],
  isFeatures: [],
  version: [],
  enabled: [],
  deleted: [],
  createTime: [],
  createdUserId: [],
  createdUserName: [],
  lastUpdatedUserId: [],
  lastUpdatedUserName: [],
  lastUpdateTime: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  processId: "",
  processCode: "",
  processName: "",
  equipId: "",
  equipCode: "",
  equipName: "",
  projectId: "",
  projectCode: "",
  projectName: "",
  modelId: "",
  modelCode: "",
  modelName: "",
  gageEquipId: "",
  gageEquipCode: "",
  gageEquipName: "",
  gageEquipMarkId: "",
  gageEquipMarkCode: "",
  gageEquipMarkName: "",
  paramId: "",
  paramCode: "",
  paramName: "",
  checkType: "",
  parameterStandards: "",
  lowerLimitValue: "",
  upperLimitValue: "",
  controlRatioUp: "",
  controlRatioDown: "",
  controlUp: "",
  controlDown: "",
  isRatioUp: false,
  isRatioDow: false,
  isSingle: false,
  isAverage: false,
  isLong: false,
  isStage: true,
  isFeatures: false,
  version: "",
  enabled: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  lastUpdateTime: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        processId,
        processCode,
        processName,
        equipId,
        equipCode,
        equipName,
        projectId,
        projectCode,
        projectName,
        modelId,
        modelCode,
        modelName,
        gageEquipId,
        gageEquipCode,
        gageEquipName,
        gageEquipMarkId,
        gageEquipMarkCode,
        gageEquipMarkName,
        paramId,
        paramCode,
        paramName,
        checkType,
        parameterStandards,
        lowerLimitValue,
        upperLimitValue,
        controlRatioUp,
        controlRatioDown,
        controlUp,
        controlDown,
        isRatioUp,
        isRatioDow,
        isSingle,
        isAverage,
        isLong,
        isStage,
        isFeatures,
        version,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      } = newVal;
      Object.assign(formData, {
        id,
        processId,
        processCode,
        processName,
        equipId,
        equipCode,
        equipName,
        projectId,
        projectCode,
        projectName,
        modelId,
        modelCode,
        modelName,
        gageEquipId,
        gageEquipCode,
        gageEquipName,
        gageEquipMarkId,
        gageEquipMarkCode,
        gageEquipMarkName,
        paramId,
        paramCode,
        paramName,
        checkType,
        parameterStandards,
        lowerLimitValue,
        upperLimitValue,
        controlRatioUp,
        controlRatioDown,
        controlUp,
        controlDown,
        isRatioUp,
        isRatioDow,
        isSingle,
        isAverage,
        isLong,
        isStage,
        isFeatures,
        version,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.processId = "";
  formData.processCode = "";
  formData.processName = "";
  formData.equipId = "";
  formData.equipCode = "";
  formData.equipName = "";
  formData.projectId = "";
  formData.projectCode = "";
  formData.projectName = "";
  formData.modelId = "";
  formData.modelCode = "";
  formData.modelName = "";
  formData.gageEquipId = "";
  formData.gageEquipCode = "";
  formData.gageEquipName = "";
  formData.gageEquipMarkId = "";
  formData.gageEquipMarkCode = "";
  formData.gageEquipMarkName = "";
  formData.paramId = "";
  formData.paramCode = "";
  formData.paramName = "";
  formData.checkType = "";
  formData.parameterStandards = "";
  formData.lowerLimitValue = "";
  formData.upperLimitValue = "";
  formData.controlRatioUp = "";
  formData.controlRatioDown = "";
  formData.controlUp = "";
  formData.controlDown = "";
  formData.isRatioUp = false;
  formData.isRatioDow = false;
  formData.isSingle = false;
  formData.isAverage = false;
  formData.isLong = false;
  formData.isStage = false;
  formData.isFeatures = false;
  formData.version = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.lastUpdateTime = "";
}

const loading = ref(false);

/** 测量特性定义保存提交 */
function handleSubmit() {
  if (formData.isRatioUp && !formData.controlRatioUp)
    return ElMessage.error("请输入控制线上限比例");
  if (formData.isRatioDow && !formData.controlRatioDown)
    return ElMessage.error("请输入控制线下限比例");
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      formData.isRatioUp = formData.isRatioUp ? 1 : 0;
      formData.isRatioDow = formData.isRatioDow ? 1 : 0;
      formData.isSingle = formData.isSingle ? 1 : 0;
      formData.isAverage = formData.isAverage ? 1 : 0;
      formData.isLong = formData.isLong ? 1 : 0;
      formData.isStage = formData.isStage ? 1 : 0;
      formData.isFeatures = formData.isFeatures ? 1 : 0;
      await request({
        url: "/dmpDosPcm/pcmmeasurefeatures/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

onMounted(() => {
  getProcessList();
  queryEquipList();
});

// 获取工序
const processList = ref([]);
const getProcessList = async () => {
  const res = await request({
    url: `/mdm/mdmprocess/getList`,
    method: "post",
    data: {},
  });
  processList.value = res.data;
};

const queryEquipList = async () => {
  const { data } = await request({
    url: "/mdm/mdmprocessequip/getList",
    method: "POST",
    data: {
      enabled: 1,
    },
  });
  const equipOptions = ref([]);
  equipOptions.value = data;
};

function controlRatioUpChange(input: any) {}

function numValidcontrolRatioUp() {
  if (formData.controlRatioUp >= 100) {
    formData.controlRatioUp = 100;
  }
  if (
    !formData.upperLimitValue ||
    !formData.lowerLimitValue ||
    formData.upperLimitValue == 0 ||
    formData.lowerLimitValue == 0
  ) {
    formData.controlUp = 0;
  } else {
    formData.controlUp =
      formData.upperLimitValue -
      (formData.upperLimitValue - formData.lowerLimitValue) *
        (1 - formData.controlRatioUp / 100);
  }
}

function numValidcontrolRatioDown() {
  if (formData.controlRatioDown >= 100) {
    formData.controlRatioDown = 100;
  }

  console.log(
    !formData.upperLimitValue ||
      !formData.lowerLimitValue ||
      formData.upperLimitValue == 0 ||
      formData.lowerLimitValue == 0
  );
  if (
    !formData.upperLimitValue ||
    !formData.lowerLimitValue ||
    formData.upperLimitValue == 0 ||
    formData.lowerLimitValue == 0
  ) {
    formData.controlDown = 0;
  } else {
    formData.controlDown =
      formData.lowerLimitValue +
      (formData.upperLimitValue - formData.lowerLimitValue) *
        (1 - formData.controlRatioDown / 100);
  }
}
</script>
